#include "../include/MainMenu.h"

using namespace godot;

void MainMenu::_register_methods() {
	register_method("_ready", &MainMenu::_ready);
	register_method("_on_SubmitButton_pressed", &MainMenu::_on_SubmitButton_pressed);
	register_method("_on_ContinueButton_pressed", &MainMenu::_on_ContinueButton_pressed);
}

void MainMenu::_init() {

}
void MainMenu::_ready() {
	get_node<Button>("VBoxContainer/SubmitButton")->connect("pressed", this, "_on_SubmitButton_pressed");
	get_node<Button>("VBoxContainer/ContinueButton")->connect("pressed", this, "_on_ContinueButton_pressed");
}

std::vector<unsigned short> MainMenu::__get_rows_and_columns() {
	std::vector<unsigned short> returnValue = {};
	try {
		unsigned short rows = std::atoi((get_node<LineEdit>("VBoxContainer/GridContainer/RowCount"))->get_text().utf8().get_data());
		if (rows <= 1 || rows > 20) {
			throw std::out_of_range("");
		}
		returnValue.push_back(rows);
	} catch (...) {
		returnValue.push_back(9);
	}
	try {
		unsigned short columns = std::atoi((get_node<LineEdit>("VBoxContainer/GridContainer/ColumnCount"))->get_text().utf8().get_data());
		if (columns <= 1 || columns > 20) {
			throw std::out_of_range("");
		}
		returnValue.push_back(columns);;
	} catch (...) {
		returnValue.push_back(9);
	}
	return returnValue;
}

void MainMenu::_on_SubmitButton_pressed() {
	get_parent()->call("_play_click1");
	std::vector<unsigned short> rcvalue = __get_rows_and_columns();
	get_parent()->call("_switch_to_new_playground", rcvalue[0], rcvalue[1]);
}
void MainMenu::_on_ContinueButton_pressed() {
	get_parent()->call("_switch_to_playground");
}